package com.ablog.service.impl;

import com.ablog.model.Article;
import com.ablog.dao.ArticleMapper;
import com.ablog.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.date.DateUtil;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Article)表服务实现类
 *
 * @author mopeiwen
 * @since 2020-07-22 13:11:20
 */
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param aid 主键
     * @return 实例对象
     */
    @Override
    public Article queryById(String aid) {
    	Article article = this.articleMapper.selectByPrimaryKey(aid);
    	
    	//访问量+1
    	article.setVisits(article.getVisits()+1);
    	this.articleMapper.updateByPrimaryKeySelective(article);
    	
        return article;
    }
    
    /**
     * 通过Article对象查询集合
     *
     * @param article 实例对象
     * @return 实例对象
     */
    @Override
    public List<Article> queryAll(Article article) {
        return this.articleMapper.selectAll(article);
    }

	@Override
	public PageInfo<Article> queryAll(Article article, int pagesize, int pagenum) {
		//pageNum代表页码值，pageSize代表每页条数
    	PageHelper.startPage(pagenum,pagesize);
    	//使用PageInfo来封装查询的数据
        PageInfo<Article> pageInfo = new PageInfo<Article>(this.articleMapper.selectAll(article));
        return pageInfo;
	}
	
	@Override
	public PageInfo<Article> queryAll(String value, int pagesize, int pagenum) {
		//pageNum代表页码值，pageSize代表每页条数
    	PageHelper.startPage(pagenum,pagesize);
    	//使用PageInfo来封装查询的数据
        PageInfo<Article> pageInfo = new PageInfo<Article>(this.articleMapper.selectAllByVal(value));
        return pageInfo;
	}
    
	@Override
	public PageInfo<Article> queryAllByTime(String starttime, String endtime, int pagesize, int pagenum) {
		//pageNum代表页码值，pageSize代表每页条数
    	PageHelper.startPage(pagenum,pagesize);
    	//使用PageInfo来封装查询的数据
        PageInfo<Article> pageInfo = new PageInfo<Article>(this.articleMapper.selectAllByTime(starttime, endtime));
		return pageInfo;
	}
	
    /**
     * 新增数据
     *
     * @param article 实例对象
     * @return 实例对象
     */
    @Override
    public Article add(Article article) {
    	article.setAddtime(DateUtil.now());
    	article.setVisits(0);//默认访问量0
        this.articleMapper.insertSelective(article);
        return article;
    }

    /**
     * 修改数据
     *
     * @param article 实例对象
     * @return 实例对象
     */
    @Override
    public Article update(Article article) {
    	article.setMtime(DateUtil.now());
        this.articleMapper.updateByPrimaryKeySelective(article);
        return this.queryById(article.getAid());
    }

    /**
     * 通过主键删除数据
     *
     * @param aid 主键
     * @return 是否成功
     */
    @Override
    public boolean delById(String aid) {
        return this.articleMapper.deleteByPrimaryKey(aid) > 0;
    }

}